package com.yocard.yxyd.ui.activity;

import android.graphics.Paint;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yocard.yxyd.utils.util.CacheDoubleStaticUtils;
import com.yocard.yxyd.utils.util.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.tuo.customview.VerificationCodeView;
import com.yocard.yxyd.R;
import com.yocard.yxyd.others.CommonTimer;
import com.yocard.yxyd.ui.activity.base.BaseActivity;
import com.yocard.yxyd.ui.contract.VerifyContract;
import com.yocard.yxyd.ui.presenter.VerifyPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 2019/4/10.
 */
public class VerifyPhoneActivity extends BaseActivity<VerifyPresenter> implements VerifyContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.input_view)
    VerificationCodeView inputView;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;

    protected String inputContent;
    protected CommonTimer myTimer;

    @Override
    protected int getLayout() {
        return R.layout.act_verify_phone;
    }

    @Override
    protected void initEventAndData() {
        initTitle();
        tvGetCode.setEnabled(true);
        tvGetCode.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tvGetCode.getPaint().setAntiAlias(true);//抗锯齿
        btnNext.setEnabled(false);
        String mobile = CacheDoubleStaticUtils.getString("mobile").trim();
        String formalMobile = mPresenter.splitMobile(mobile);
        tvMobile.setText(formalMobile);

        inputView.setInputCompleteListener(new VerificationCodeView.InputCompleteListener() {

            @Override
            public void inputComplete() {
                inputContent = inputView.getInputContent();
                if(inputContent.length() == 4){
                    btnNext.setEnabled(true);
                }else {
                    btnNext.setEnabled(false);
                }
            }

            @Override
            public void deleteContent() {
                inputContent = inputView.getInputContent();
                if(inputContent.length() == 4){
                    btnNext.setEnabled(true);
                }else {
                    btnNext.setEnabled(false);
                }
            }
        });

        myTimer = new CommonTimer(60 * 1000, 1000, tvGetCode,"重新发送");
    }

    private void initTitle() {
        ImmersionBar.with(this).statusBarColor(R.color.white)
                .statusBarDarkFont(true).navigationBarColor(R.color.white).init();
        tvTitle.setText("安全验证");
    }

    @OnClick({R.id.iv_back,R.id.btn_next,R.id.tv_get_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ActivityUtils.finishActivity(this);
                break;
            case R.id.btn_next:
                showLoadingDialog();
                mPresenter.verifyOldCode(inputContent);
                break;
            case R.id.tv_get_code:
                //防止短时间重复点击
                if (isFastDoubleClick()) return;

                showLoadingDialog();
                mPresenter.getOldMobileCode();
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onGetOldSuccess() {
        dismisDialog();
        tvGetCode.setEnabled(false);
        myTimer.start();
        ToastUtils.showShort("获取验证码成功");
    }

    @Override
    public void onOldCodePush() {
        dismisDialog();
        ActivityUtils.startActivity(ChangePhoneActivity.class);
        ActivityUtils.finishActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(myTimer != null){
            myTimer.cancel();
        }
    }
}
